home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / SOURCES / RUBBAND.C < prev    next >
C/C++ Source or Header  |  1980-01-03  |  3KB  |  113 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Rubberbanding primitives.
  25.  */
  26.  
  27. #include <InterViews/rubband.h>
  28. #include <InterViews/painter.h>
  29. #include <math.h>
  30.  
  31. static float DEGS_PER_RAD = 180.0 / M_PI;
  32.  
  33. float Rubberband::Angle (Coord x0, Coord y0, Coord x1, Coord y1) {
  34.     float dx, dy, angle;
  35.  
  36.     dx = float(x1 - x0);
  37.     dy = float(y1 - y0);
  38.     if (dx != 0) {
  39.         angle = DEGS_PER_RAD * atan(dy/dx);
  40.     } else if (dy < 0) {
  41.         angle = -90;
  42.     } else {
  43.         angle = 90;
  44.     }
  45.     if (dx < 0) {
  46.         angle += 180;
  47.     }
  48.     return angle;
  49. }
  50.  
  51. float Rubberband::Distance (Coord x0, Coord y0, Coord x1, Coord y1) {
  52.     float dx = x0 - x1;
  53.     float dy = y0 - y1;
  54.  
  55.     return sqrt(dx*dx + dy*dy);
  56. }
  57.  
  58. Rubberband::Rubberband (Painter* p, Canvas* c, Coord x, Coord y) {
  59.     if (p == nil) {
  60.     output = nil;
  61.     } else {
  62.     output = new Painter(p);
  63.     output->Begin_xor();
  64.     }
  65.     canvas = c;
  66.     drawn = false;
  67.     offx = x;
  68.     offy = y;
  69. }
  70.  
  71. void Rubberband::Draw () {
  72.     /* shouldn't be called */
  73. }
  74.  
  75. void Rubberband::Redraw () {
  76.     drawn = false;
  77.     Draw();
  78. }
  79.  
  80. void Rubberband::Erase () {
  81.     if (drawn) {
  82.         drawn = false;
  83.         Draw();
  84.         drawn = false;
  85.     }
  86. }
  87.  
  88. void Rubberband::Track (Coord x, Coord y) {
  89.     if (x != trackx || y != tracky) {
  90.         Erase();
  91.     trackx = x;
  92.     tracky = y;
  93.     Draw();
  94.     }
  95. }
  96.  
  97. Rubberband::~Rubberband () {
  98.     Unref(output);
  99. }
  100.  
  101. void Rubberband::SetPainter (Painter* p) {
  102.     if (p != output) {
  103.     p->Reference();
  104.     Unref(output);
  105.     output = p;
  106.     output->Begin_xor();
  107.     }
  108. }
  109.  
  110. void Rubberband::SetCanvas (Canvas* c) {
  111.     canvas = c;
  112. }
  113.